import 'package:changxindianzishangwu/dao/user_info_dao.dart';
import 'package:changxindianzishangwu/model/user_info_model.dart';
import 'package:changxindianzishangwu/provider/user.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_user_full_info.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_value_callback.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';

class SetAutographPage extends StatefulWidget {
  String name;
  SetAutographPage({Key key, this.name}) : super(key: key);

  @override
  _SetAutographPageState createState() => _SetAutographPageState();
}

class _SetAutographPageState extends State<SetAutographPage> {
  TextEditingController _controller;
  @override
  void initState() {
    _controller=new TextEditingController(text: widget.name);
    // TODO: implement initState
    super.initState();
  }
  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    var autograph = "";
    return Scaffold(
      appBar: TopAppBar(
        context,
        "设置签名",
        rightText: "完成",
        isRightTextBtn: true,
        rightCallback: () {
          autograph=_controller.text.toString().trim();
          if (autograph == "") {
            toast("请输入签名");
            return;
          }
          EasyLoading.show(status: "修改中...");

          TencentImSDKPlugin.v2TIMManager
              .setSelfInfo(
            userFullInfo: V2TimUserFullInfo.fromJson(
              {
                "selfSignature": autograph,
              },
            ),
          )
              .then((value) {
            if (value.code == 0) {
              onSelfInfoUpdated();
              _updateUserInfo(autograph);
            } else {
              EasyLoading.dismiss();
            }
          });

          //todo 需要走服务器流程
          print(widget.name);
          print(autograph);
        },
      ),
      body: Column(
        children: [
          GestureDetector(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Container(
              padding: EdgeInsets.only(left: 20),
              margin: EdgeInsets.only(
                  left: ScreenUtil().setWidth(15),
                  top: ScreenUtil().setHeight(10),
                  right: ScreenUtil().setWidth(15)),
              decoration: BoxDecoration(
                  color: Color.fromRGBO(241, 241, 241, 1),
                  borderRadius: BorderRadius.circular(6)),
              child: TextField(
                cursorColor: Colors.lightBlueAccent,
                autofocus: true,
                maxLines: 1,
                maxLength: 30,
               controller: _controller,
                decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText: widget.name,
                    hintStyle: TextStyle(fontSize: 15)),
              ),
            ),
          ),
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(right: 20, top: 10),
            child: Text(
              "30个字符以内",
              style: TextStyle(
                fontSize: ScreenUtil().setSp(12),
                color: Color.fromRGBO(153, 153, 153, 1),
              ),
            ),
          ),
        ],
      ),
    );
  }

  void onSelfInfoUpdated() async {
    //自己信息更新，从新获取自己的信息；
    V2TimValueCallback<String> usercallback =
        await TencentImSDKPlugin.v2TIMManager.getLoginUser();
    V2TimValueCallback<List<V2TimUserFullInfo>> infos = await TencentImSDKPlugin
        .v2TIMManager
        .getUsersInfo(userIDList: [usercallback.data]);
    if (infos.code == 0) {
      Provider.of<UserModel>(context, listen: false).setInfo(infos.data[0]);
    }
  }

  _updateUserInfo(String content) async {
    try {
      UserInfoModel userInfoModel =
          await UserInfoDao.updateUserInfo("description", content);
      EasyLoading.dismiss();
      if (userInfoModel.resultCode == 0) {
        toast(userInfoModel.resultMsg);
        return;
      }
      Navigator.of(context).pop();
      toast("修改成功");
    } catch (e) {
      print(e);
      _error();
    }
  }

  void _error() {
    toast("修改失败");
    EasyLoading.dismiss();

  }
}
